import React, { useEffect } from "react";
import { useLocation, useHistory } from "react-router-dom";
export default function Cussect(Com: any) {
  return function submit(props: any) {
    let location = useLocation();
    let history = useHistory();
    useEffect(() => {
      if (location.pathname !== "/login") {
        let token = localStorage.getItem("token");
        if (!token) {
          history.push("/login");
        }
      } else if (location.pathname == "/login") {
        let token = localStorage.getItem("token");
        if (token) {
          history.push("/admin/home");
        }
      }
    });
    return (
      <div>
        <Com {...props} />
      </div>
    );
  };
}
